package es.aa.android.cartomatic.implementations.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import es.aa.android.cartomatic.R;
import es.aa.android.cartomatic.domain.Card;
import es.aa.android.cartomatic.domain.Deck;
import es.aa.android.cartomatic.domain.Options;
import es.aa.android.cartomatic.implementations.da.SQLiteAccess;

public class BoardService {
	
	private Options options;
	private SQLiteAccess acceso = new SQLiteAccess();
	private List<List<Card>> hands = new ArrayList<List<Card>>();
	private List<Card> aux = new ArrayList<Card>();
	private List<Deck> decks = new ArrayList<Deck>();
	private Intent log;
	private String LOG;
	private int idDeck[];
	
	public BoardService(Activity activity, Options opciones) {
		options = opciones;
		LOG = activity.getResources().getString(R.string.log);
		log = activity.getIntent();
		log.putExtra(LOG, "Se ha iniciado la partida.");
		idDeck =opciones.getDecksIDS();
		
		for (int i = 0; i < idDeck.length; i++) {
			initDeck(activity.getBaseContext(), idDeck[i]);
		}
		
	}
	
	// Getters and Setters
	public List<List<Card>> getHands() {
		return hands;
	}


	public void setHands(List<List<Card>> hands) {
		this.hands = hands;
	}


	public List<Deck> getDecks() {
		return decks;
	}


	public void setDecks(List<Deck> decks) {
		this.decks = decks;
	}


	
	//mostrar una baraja especifica
	public Deck verColeccion(int numDeck){
		log.putExtra(LOG, "Ha visto las cartas de la baraja.");
		return decks.get(numDeck);
	}
	
	//barajear
	public List<?> shuffle(List<?> lista){
		Collections.shuffle(lista);
		//Collections.shuffle(decks.get(deck));
		//log.putExtra(LOG, "Ha barajeado.");
		//return decks.get(deck);
		return lista;
	}

	
	//recoge la lista de cartas contenidas en una baraja, mediante un ID.
	//inicializa las distintas barajas, primero barajea lo recogido y luego almacena la baraja como jugable.
	
	public void initDeck(Context context, int id){
		aux = new ArrayList<Card>();
		Deck baraja = new Deck();
		baraja = acceso.readDeck(context, id);
		decks.add(baraja);
	}
	
	public List<Card> nextPlayer(int num){
		aux = new ArrayList<Card>();
		aux = hands.get(num);
		log.putExtra(LOG, "Ha pasado al siguiente turno.");
		return aux;
	}
	
	public Options getOptions() {
		return options;
	}
	
	//Roba cartas de la baraja indicada
	/*public void stealCards(int deck, int player){

			hands.get(player).add(decks.get(deck).get(decks.get(deck).size()));
			decks.get(deck).remove(decks.get(deck).get(decks.get(deck).size()));
			log.putExtra(LOG, "Ha robado una carta.");
			
	}*/
	
	
	//Inicializa las manos para 1 o mas jugadores dependiendo del numero de barajas
	/*public void initHands() {
		if(options.getNum_Players() > options.getNum_Decks()){
			
			for (int i = 0; i < options.getNum_Players(); i++) {
				
				for(int x = 0; x < options.getNum_InitCards(); x++){
					
					stealCards(0, i);
					
				}
				
			}
			
		}else{
			
			for (int i = 0; i < options.getNum_Decks(); i++) {
				
				for(int x = 0; x < options.getNum_InitCards(); x++){
					
					stealCards(i, i);
					
				}
			}
		}
	}*/
	
}
